package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.entity.WithinLetter;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.service.WithinLetterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: <br/>
 * 栈内信
 * <p>
 * <br/>
 * @Auther: walter
 * @create 2020/05/07 10:34
 */
@RestController
@RequestMapping("within/letter")
public class WithinLetterController {

    /**
     * 栈内信服务
     */
    @Autowired
    private WithinLetterService withinLetterService;

    /**
     * 列表
     *
     * @return Result
     */
    @AutoLog
    @GetMapping("/list")
    public Result list() throws AlertException {
        String userId = SysUtils.currentUserId();
        List<WithinLetter> list = withinLetterService.list(SysUtils.autoCnd(WithinLetter.class).eq(WithinLetter.RECEIVE_ID, userId)
                .orderByAsc(WithinLetter.STATE)
                .orderByDesc(WithinLetter.CREATE_TIME));


        List<WithinLetter> noReadList = withinLetterService.list(new QueryWrapper<WithinLetter>()
                .eq(WithinLetter.RECEIVE_ID, userId)
                .eq(WithinLetter.STATE, 0));
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("num", noReadList.size());
        return Result.ok().data(map);
    }


    /**
     * 添加栈内信
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PostMapping
    public Result add(WithinLetter withinLetter) throws AlertException {
        boolean b = withinLetterService.save(withinLetter);
        if (!b) {
            throw new AlertException("栈内信添加失败");
        }
        return Result.ok("栈内信添加成功");
    }


    /**
     * 获得指定id 栈内信 同时已读。
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") String id) throws AlertException {
        WithinLetter withinLetter = withinLetterService.getById(id);
        if (withinLetter == null) {
            throw new AlertException("未找到相关栈内信信息");
        }
        return Result.ok().data(withinLetter);
    }


    /**
     * 更新栈内信
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PutMapping
    public Result update(WithinLetter withinLetter) throws AlertException {
        boolean b = withinLetterService.updateById(withinLetter);
        if (!b) {
            throw new AlertException("栈内信更新失败");
        }

        return Result.ok("栈内信更新成功");
    }


    /**
     * 删除栈内信
     *
     * @param
     * @return Result
     */
    @AutoLog
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) throws AlertException {
        boolean b = withinLetterService.removeById(id);

        if (!b) {
            throw new AlertException("栈内信删除失败");
        }

        return Result.ok("删除栈内信成功");
    }

    @AutoLog
    @GetMapping("/read")
    public Result read(@RequestParam("id") Integer id) throws AlertException {
        WithinLetter letter = withinLetterService.getOne(new QueryWrapper<WithinLetter>()
                .eq(WithinLetter.ID, id));
        letter.setState(1);
        boolean b = withinLetterService.updateById(letter);
        if (!b) {
            throw new AlertException("操作失败");
        }
        return Result.ok();
    }

    @AutoLog
    @GetMapping("/unReadNum")
    public Result unReadNum() throws AlertException {
        String userId = SysUtils.currentUserId();
        List<WithinLetter> list = withinLetterService.list(new QueryWrapper<WithinLetter>()
                .eq(WithinLetter.RECEIVE_ID, userId)
                .eq(WithinLetter.STATE, 0));
        Map<String, Object> map = new HashMap<>();
        map.put("num", list.size());
        return Result.ok().data(map);
    }

    @AutoLog
    @GetMapping("/send")
    public void send() throws AlertException {
        String userId = SysUtils.currentUserId();
        for (int i = 0; i < 100; i++) {
            withinLetterService.sendToOne(userId, "1248137224181047297", "测试" + i, "测试内容" + i, i % 2 == 0 ? 0 : 1);
        }
    }


}
